<?php

require './includes/config.inc.php';
class Employer
{
    // Allow access by all methods
    public $conn;
    // This is a magic method called automatically when class is used
    public function __construct()
    {
            $this->conn = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME)
                    or die('I\'m sorry but the server has failed. Please check back soon!');
    }
<<<<<<< .mine
	
	/*Collect Active Employer Details*/
=======
	
	// Remove Employer from Active Employer list
	public function deleteEmployer()
	{
	}
	
>>>>>>> .r35
    public function getEmployerDetails()
    {
        $sql = 'SELECT id,companyName, contactName, email, isAdmin FROM employers WHERE deleted = 0;';
			
        if($stmt = $this->conn->prepare($sql))
        {
            $stmt->execute();
            $stmt->bind_result($id, $companyName, $contactName, $email, $isAdmin);
            $collectEmps = array();
            while ($stmt->fetch()) {
                $empArray = array(  'id'=>$id,
									'companyName'=>$companyName,
                                    'contactName'=>$contactName,
									'email'=>$email,
                                    'isAdmin'=>$isAdmin);
                array_push($collectEmps, $empArray);
            }
            return $collectEmps;
        }
        else
        {
            return false;
        }
    }
	
	/*Collect Specific Employer Details*/
    public function getSpecificEmployerDetails($id)
    {
        $sql = 'SELECT id,companyName, contactName, email, isAdmin FROM employers WHERE id = ?;';
			
        if($stmt = $this->conn->prepare($sql))
        {
			$stmt->bind_param('i',$id);
			$stmt->execute();
            $stmt->bind_result($id, $companyName, $contactName, $email, $isAdmin);
            $collectInfo = array();
            if($stmt->fetch()) {
                $empArray = array(  'id'=>$id,
									'companyName'=>$companyName,
                                    'contactName'=>$contactName,
									'email'=>$email,
                                    'isAdmin'=>$isAdmin);
                array_push($collectInfo, $empArray);
            }
            return $collectInfo;
        }
        else
        {
            return false;
        }
    }
	
	/*Collect InActive Employer Details*/
	public function getInactiveEmployerDetails()
    {
        $sql = 'SELECT id,companyName, contactName, email, isAdmin FROM employers WHERE deleted = 1;';
			
        if($stmt = $this->conn->prepare($sql))
        {
            $stmt->execute();
            $stmt->bind_result($id, $companyName, $contactName, $email, $isAdmin);
            $collectEmps = array();
            while ($stmt->fetch()) {
                $empArray = array(  'id'=>$id,
									'companyName'=>$companyName,
                                    'contactName'=>$contactName,
									'email'=>$email,
                                    'isAdmin'=>$isAdmin);
                array_push($collectEmps, $empArray);
            }
            return $collectEmps;
        }
        else
        {
            return false;
        }
    }

    /*DeActivate Active Employer*/
	public function removeUser($userId)
	{
		$sql = "UPDATE employers
				SET deleted = 1 
				WHERE id = ? ";
		if($stmt = $this->conn->prepare($sql))
		{
           $stmt->bind_param('i',$userId);
           $stmt->execute();
		   $stmt->close();
		   	require_once 'classes/Login.php';
    		$Login = new Login();
			/*include 'includes/header.inc.php';*/

			header('location:myEmployers.php');
			/*include 'includes/footer.inc.php';*/
		}
		else
		{
			return 'ERROR! User Not Deleted!';
		}
	}
	
	/*DeActivate User Posts*/
	public function removeAllPosts($userId)
	{
		$sql = "UPDATE job_posts
				SET deleted = 1 
				WHERE employerID = ? ";
		if($stmt = $this->conn->prepare($sql))
		{
           $stmt->bind_param('i',$userId);
           $stmt->execute();
		   $stmt->close();
		   	require_once 'classes/Login.php';
    		$Login = new Login();
			/*include 'includes/header.inc.php';*/

			header('location:myEmployers.php');
			/*include 'includes/footer.inc.php';*/
		}
		else
		{
			return 'ERROR! User Posts Not Deleted!';
		}
	}
	
	/*DeActivate Active Employer*/
	public function restoreUser($userId)
	{
		$sql = "UPDATE employers
				SET deleted = 0 
				WHERE id = ? ";
		if($stmt = $this->conn->prepare($sql))
		{
           $stmt->bind_param('i',$userId);
           $stmt->execute();
		   $stmt->close();
		   	require_once 'classes/Login.php';
    		$Login = new Login();
			/*include 'includes/header.inc.php';*/

			header('location:myEmployers.php');
			/*include 'includes/footer.inc.php';*/
		}
		else
		{
			return 'ERROR! User Not Restored!';
		}
	}
	
	/*ReActivate User Posts*/
	public function restoreAllPosts($userId)
	{
		$sql = "UPDATE job_posts
				SET deleted = 0 
				WHERE employerID = ? ";
		if($stmt = $this->conn->prepare($sql))
		{
           $stmt->bind_param('i',$userId);
           $stmt->execute();
		   $stmt->close();
		   	require_once 'classes/Login.php';
    		$Login = new Login();
			/*include 'includes/header.inc.php';*/

			header('location:myEmployers.php');
			/*include 'includes/footer.inc.php';*/
		}
		else
		{
			return 'ERROR! User Posts Not Restored!';
		}
	}
	
	/*Promote User to Employer Administrator*/
	public function promoteUser($userId)
	{
		$sql = "UPDATE employers
				SET isAdmin = 1 
				WHERE id = ? ";
		if($stmt = $this->conn->prepare($sql))
		{
           $stmt->bind_param('i',$userId);
           $stmt->execute();
		   $stmt->close();
		   	require_once 'classes/Login.php';
    		$Login = new Login();
			/*include 'includes/header.inc.php';*/

			header('location:myEmployers.php');
			/*include 'includes/footer.inc.php';*/
		}
		else
		{
			return 'ERROR! User Admin status Not Updated!';
		}
	}
	
	/*Demote User to Employer Administrator*/
	public function demoteUser($userId)
	{
		$sql = "UPDATE employers
				SET isAdmin = 0 
				WHERE id = ? ";
		if($stmt = $this->conn->prepare($sql))
		{
           $stmt->bind_param('i',$userId);
           $stmt->execute();
		   $stmt->close();
		   	require_once 'classes/Login.php';
    		$Login = new Login();
			/*include 'includes/header.inc.php';*/

			header('location:myEmployers.php');
			/*include 'includes/footer.inc.php';*/
		}
		else
		{
			return 'ERROR! User Admin status Not Updated!';
		}
	}
	
	
	/*Is this the last Power User?*/
	public function isLast()
	{
		$sql = " SELECT count( * ) as adminCount 
            	 FROM employers as emp
                 WHERE emp.isAdmin = 1 ";
		if($stmt = $this->conn->prepare($sql))
		{
			$stmt->execute();
			$stmt->bind_result($adminCount);
			if($stmt->fetch())
            {
                if($adminCount == 1)
					{ return true; }
				else
					{ return false; }
            }
			else { return 'ERROR!'; }
			
		}
	}
	
	/*Employer Count*/
	public function count_all() 
	{
	   $sql = "SELECT count( * ) as adminCount 
            	 FROM employers 
				 WHERE deleted = 0";
	   
	   if($stmt = $this->conn->prepare($sql))
	   {
			$stmt->execute();
			$stmt->bind_result($adminCount);
			if($stmt->fetch())
            	return($adminCount);
		}
	}
	
	
	/*Employer Count*/
	public function count_inactive() 
	{
	   $sql = "SELECT count( * ) as adminCount 
            	 FROM employers 
				 WHERE deleted = 1";
	   
	   if($stmt = $this->conn->prepare($sql))
	   {
			$stmt->execute();
			$stmt->bind_result($adminCount);
			if($stmt->fetch())
            	return($adminCount);
		}
	}


    public function __destruct()
    {
            // Close the connection, return true if successful
            $closeConnection = $this->conn->close();
            if($closeConnection){return true;}
    }
}
?>